Ext.define('StaffServer.controller.SalaryChangeController', {
	extend : 'Ext.app.Controller',

	models: ['TeacherModel','SalaryChangeModel'],
	stores: ['TeacherStore','SalaryChangeStore'],
	views:  [
	//'salarychange.SalaryChangeGrid', 
	         'salarychange.SalaryChangeMngr',
	         'salarychange.SalaryChangeSearchPanel',
	         'salarychange.SalaryChangeDetailGrid',
	         //'salarychange.SalaryChangeWindow',
	 
	         'salarychange.SalaryChangeDetailWindow',
	         'salarychange.SalaryChangeDetailForm',
	         'salarychange.SalaryChangeExcelForm',
	         'salarychange.SalaryChangeExcelWindow'
	         ],
	
	tabView: 'StaffServer.view.salarychange.SalaryChangeMngr',
	salaryChangeDetailWindow:null,
	salaryChangeDetailForm:null,
	//salaryChangeWindow:null,
	salaryChangeForm:null,
	salaryChangeExcelWindow:null,
	salaryChangeExcelForm:null,
	submitFlag: null,
	retireId: null,
	staffRecord: null,
	refs : [ {
		ref : 'salaryChangeSearchPanel',
		selector : 'salaryChangeSearchPanel'
	}, {
		ref : 'salaryChangeDetailGrid',
		selector : 'salaryChangeDetailGrid'
	}],

	// 初始化执行语句
	init : function() {                  
		//salaryChangeWindow = Ext.widget("salaryChangeWindow");
		salaryChangeDetailWindow= Ext.widget("salaryChangeDetailWindow");
		salaryChangeDetailForm = Ext.ComponentQuery.query('salaryChangeDetailForm')[0];
		salaryChangeExcelWindow = Ext.widget("salaryChangeExcelWindow");
		salaryChangeExcelForm = Ext.ComponentQuery.query('salaryChangeExcelForm')[0];
		
		// 控制事件
		this.control({
			'salaryChangeSearchPanel button[action=search]' : {
				click : this.search
			},
			'salaryChangeSearchPanel button[action=reset]' : {
				click : this.reset
			},
			
			'salaryChangeDetailGrid button[action=saveSalaryChange]' : {
				click : this.saveSalaryChange
			},
			'salaryChangeDetailWindow button[action=close]' : {
				click : this.closeDetailWindow
			},
			'salaryChangeDetailWindow button[action=submit]' : {
				click : this.submitDetialWindow
			},
			'salaryChangeDetailGrid button[action=deleteSalaryChange]' : {
				click : this.deleteSalaryChange
			},
			'salaryChangeDetailGrid button[action=updateSalaryChange]' : {
				click : this.updateSalaryChange
			},
//			'salaryChangeWindow button[action=close]' : {
//				click : this.closeWindow
//			},			
			'salaryChangeDetailGrid button[action=exportExcel]' : {
				click : this.exportExcel
			},
			'salaryChangeDetailGrid button[action=importExcel]' : {
				click : this.importExcel
			},
			 
			'salaryChangeExcelWindow button[action=submit]' : {
				click : this.submitExcelWindow
			},
			'salaryChangeExcelWindow button[action=close]' : {
				click : this.closeExcelWindow
			}
		});
	},
	//关闭窗口
	closeExcelWindow: function(){
		salaryChangeExcelForm.getForm().reset();
		salaryChangeExcelWindow.hide();
	},
	
	//提交表单
	submitExcelWindow: function(){
		if (!salaryChangeExcelForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = salaryChangeExcelForm.getValues();
		salaryChangeExcelForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: 'salarychange/importExcel',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeExcelWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//导入
	importExcel: function(){
		salaryChangeExcelWindow.setTitle("导入文件");
		salaryChangeExcelWindow.show();
	},
	exportExcel: function(){
		var searchPanel = this.getSalaryChangeSearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'salarychange/exportSalaryChangeExcel?jsonFilter=' + jsonFilter;
	},
	
	
//	//关闭窗口
//	closeWindow: function(){
//		salaryChangeWindow.hide();
//	},
	
	
	//更新
	updateSalaryChange: function(){
		var record = this.getSalaryChangeDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        salaryChangeDetailForm.loadRecord(record);
        submitFlag = "update";
        salaryChangeDetailWindow.setTitle("修改员工薪酬变更信息");
        salaryChangeDetailWindow.show();
	},
	
	closeDetailWindow:function(){
		salaryChangeDetailForm.getForm().reset();
		salaryChangeDetailWindow.hide();
		
	},
	
	
	//删除
	deleteSalaryChange: function(){
		var $this = this;
		var record = this.getSalaryChangeDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'salarychange/deleteSalaryChange',
                    params: {
                    	sn: record.get("sn")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.getSalaryChangeDetailGrid().getStore().reload();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	
	//提交表单
	submitDetialWindow: function(){
		if (!salaryChangeDetailForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = salaryChangeDetailForm.getValues();
		salaryChangeDetailForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		 url: submitFlag == 'save' ? 'salarychange/saveSalaryChange' : 'salarychange/updateSalaryChange',
		//	 url: submitFlag == 'save' ? 'degreeStudy/saveDegreeStudy' : 'degreeStudy/updateDegreeStudy',
			params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeDetailWindow();
		    		$this.getSalaryChangeDetailGrid().getStore().reload();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//新增
	saveSalaryChange: function(){
		submitFlag = "save";
		salaryChangeDetailWindow.setTitle("新增员工薪酬变更信息");
		salaryChangeDetailWindow.show();
		//Ext.ComponentQuery.query('salaryChangeDetailForm field[name="staffId"]')[0].setReadOnly(true);
		//salaryChangeDetailForm.loadRecord(staffRecord);
	},
//	salaryChangeInfo: function() {
//		var record = this.getSalaryChangeGrid().getSelectionModel().getLastSelected();
//        if (Ext.isEmpty(record)) {
//            Ext.Msg.alert('提示', '请先选择要操作的记录!');
//            return;
//        }
//		var staffId = record.get("staffId");
//		staffRecord = record;
//		var salaryChangeStore = this.getSalaryChangeDetailGrid().getStore();
//		salaryChangeStore.on('beforeload', function(store){
//			  var sparams = {search_EQ_staffId: staffId};
//        	Ext.apply(store.proxy.extraParams, sparams);
//        });
//		salaryChangeStore.load();
//		salaryChangeWindow.setTitle("员工薪酬变更信息");
//		salaryChangeWindow.show();
//	},
//	
	
	//searchPanel查询
	search : function() {
		var searchPanel = this.getSalaryChangeSearchPanel();
		var grid = this.getSalaryChangeDetailGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('salaryChangeDetailGrid > pagingtoolbar')[0].moveFirst();
	},
	
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getSalaryChangeSearchPanel();
		searchPanel.getForm().reset();
	}
	

});